﻿CREATE TRIGGER [TipVieweds_insert_trigger] ON [TipVieweds] FOR INSERT AS
UPDATE [side] SET [sync_row_is_tombstone] = 0, [local_update_peer_key] = 0, [restore_timestamp] = NULL, [update_scope_local_id] = NULL, [last_change_datetime] = GETDATE(), [UserID] = [i].[UserID] FROM [TipVieweds_tracking] [side] JOIN INSERTED AS [i] ON [side].[TipViewedID] = [i].[TipViewedID]
INSERT INTO [TipVieweds_tracking] ([i].[TipViewedID], [create_scope_local_id], [local_create_peer_key], [local_create_peer_timestamp], [update_scope_local_id], [local_update_peer_key], [sync_row_is_tombstone], [last_change_datetime], [restore_timestamp], [i].[UserID]) SELECT [i].[TipViewedID], NULL, 0, @@DBTS+1, NULL, 0, 0, GETDATE() , NULL, [i].[UserID] FROM INSERTED AS [i] LEFT JOIN [TipVieweds_tracking] [side] ON [side].[TipViewedID] = [i].[TipViewedID] WHERE [side].[TipViewedID] IS NULL